An Introduction to R

Why Learn to Code?

Author
Affiliation

Julien Martin

University of Ottawa

Published

April 4, 2024

0.1 Learning outcomes

this course (hopefully) will …

  • introduce you to using R ✔️

  • help your research become more robust and reproducible ✔️

this course (definitely) won’t …

  • teach you everything there is to know about R ❌

  • make you feel completely comfortable with R ❌

0.2 What is R?

  • environment for statistical computing, graphics and programming

  • created by Ross Ihaka and Robert Gentleman (1996)

  • maintained by international R-core development team

  • many, people contribute to R and the wider R community

0.3 extra R information

0.4 why is R considered hard?

  • high initial investment in time to learn R

  • unfamiliar command line environment

  • frustrating (and sometimes) inconsistent syntax

  • analysis and figures can take longer (initially!)

  • relatively steep learning curve

  • it’s not actually that hard, just unfamiliar

0.5 why you should learn R

  • it’s free and platform independent

  • it’s the software of choice for many students, academics, industries and charities worldwide

  • highly flexible and extensive

  • encourages you think about your research
    questions, data and analyses

0.6 why you should learn R

  • it allows you to keep an exact and reproducible record of your analyses

  • transparent

  • other people1 can
    reproduce your analysis

  • easily share your code (GitHub)

  • open science

0.7 why you should learn R

  • excellent graphics capabilities

0.8 why you should learn R

  • employability

  • opportunity to get involved
    with a fantastic and supportive
    community

1 Interacting with R

1.1 GUI

#


R

IDE

1.2 RStudio

1.3 VS Code

2 Using R

2.1 objects

  • commands are typed in the editor and then sourced into the console
    at the > prompt
2 + 2
[1] 4
  • R is object orientated. You can create variable and assign value(s) to them
a <- 2 + 2
a
[1] 4

2.2 objects

  • once created, operations can be performed on variables
a <- 2 + 2
b <- 3 * 2

a + b
[1] 10
  • very powerful and flexible

2.3 functions

  • functions contain a set of instructions that allow you to perform a specific task(s)
1numbers <- c(2, 3, 4, 5, 6)
numbers

2mean(numbers)
3# estimating the variance
4var(numbers)
1
use the concatenate function
2
use mean function
3
this is a comment noted with #
4
use variance function

2.4 functions

  • you can install user contributed packages to increase versatility and power

  • there are packages for almost anything

  • install packages from CRAN, Bioconductor and GitHub

  • packages are easy to install in R

2.5 functions

  • Write your own functions

  • function to calculate standard error

1se_fnc <- function(x) {
2  std.x <- sd(x)
  nos.x <- length(x)
  se.x <- std.x / (sqrt(nos.x))
3  print(se.x)
}
4se_fnc(c(2, 3, 4, 5, 6))
1
define function name and arguments
2
calculate SD, number of obs, then SE
3
define function output
4
use your new function

2.6 syntax

  • R is case sensitive A is not the same as a

  • commands are generally separated by a new line, but you can also use a ; (rare)

  • anything that follows the hash symbol # will be ignored by R. Use this to comment your code

  • a series of commands can be grouped using braces { }

  • write pretty code by following a code style guide

2.7 help

  • when you need help for a function use help() or ?
help("plot")    # open help file for the function plot

or equivalently

?plot
  • or to search across all help files use help.search("plot") or ??plot

2.8 help

  • Duckduckgo_it: Try online search You’ll be surprised how many other people have probably had the same problem and solved it.

  • Stack Overflow: There are thousands of questions relevant to R on Stack Overflow

  • Here are the most popular ones, ranked by vote.

  • make sure you include a reproducible example to get the most useful advice. A reproducible example is a minimal example that lets others who are trying to help you to see the error themselves.

2.9 tips

  • use R regularly

  • learning R is not a memory test, you have your R scripts

  • you don’t need to know everything about R

  • don’t stare at code for hours.

  • there are many ways to tackle a particular problem

the time invested now will be more than payed back in the future.

3 Thanks!

Credit: I borrowed slides from Alex Douglas.

Footnotes

  1. including the future you!↩︎